<template>
  <view class="model_box">
    <view v-for="(item, index) in itemModel" :key="index" class="model" :class="index === itemModel.length - 1 ? 'border-none' : ''" @click="item.input == 'select' ? showTree() : ''">
      <view class="label">{{ item.label }}</view>
      <u-input :type="item.input" class="value" :placeholder="item.placeholder" :disabled="item.disabled" border="none" disabledColor="white" inputAlign="right" v-model="item.value" @change="change(item, index)"></u-input>
      <view slot="right" v-if="item.input == 'select'">
        <u-icon name="arrow-right"></u-icon>
      </view>
    </view>
    <next-tree ref="qiantree" :selectParent="true" labelKey="name" valueKey="id" codeKey="code" :multiple="false" :treeData="subjectList" @confirm="onconfirm" />
  </view>
</template>

<script>
import nextTree from '../components/tree.vue'
export default {
  components: { nextTree },
  props: {
    itemModel: {
      type: Array,
      default: () => [],
    },
    subjectList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      show: false,
      columnData: [],
      selectItem: '',
    }
  },

  methods: {
    showTree() {
      this.$refs.qiantree._show()
    },
    onconfirm(list) {
      console.log('选中项的数量列表list：', list)
      this.itemModel[this.itemModel.length - 1].value = list.name[0]
      // console.log(this.itemModel[2].value)
      this.$emit('send', { data: { value: list.name[0] }, index: 2 })
      this.$emit('sendParentTree', { name: list.name[0], code: list.code[0], id: list.id[0] })
    },
    change(item, index) {
      console.log('change', item)
      this.$emit('send', { data: item, index: index })
    },
    changeHandler(e) {
      const { columnIndex, index, indexs, picker = this.$refs.uPicker } = e

      if (columnIndex === 0) {
        if (this.subjectList[columnIndex][index].children) {
          const children1 = this.subjectList[columnIndex][index].children
          picker.setColumnValues(1, children1)
        }
      }
    },
    confirm(e) {
      console.log(e)

      this.itemModel[this.itemModel.length - 1].value = e.value[1].name ? e.value[1].name : e.value[0].name
      console.log(this.itemModel[2].value)

      this.$emit('send', { data: { value: this.itemModel[2].value }, index: 2 })
      this.show = false
    },
  },
}
</script>

<style scoped lang="scss">
.model_box {
  background-color: white;
  border-radius: 10rpx;
  padding: 20rpx;
  margin: 20rpx 0;
  .model {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1rpx solid #f1f1f1;
    height: 80rpx;

    .label {
      font-size: 28rpx;
    }
    .value {
      font-size: 28rpx;
    }
  }
  .border-none {
    border: none !important;
  }
}
</style>
